package com.esun.cheetah.view.setting.notice;

import java.util.Random;

import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.IntentKey;
import com.esun.cheetah.view.match.matchinfo.MatchInfoAct;
import com.esun.cheetah.view.setting.ValuesManager;

public class Notifier {
	private static final Random random = new Random(System.currentTimeMillis());
	private Context context;
	private SharedPreferences sharedPrefValues;
	private ValuesManager valuesManager;
	private NotificationManager notificationManager;
	private int count = 0;
	private SharedPreferences pushNumber;
	private String content;
	private KeyguardManager  keyguardManager;
	public Notifier(Context context) {
		this.context = context;
		this.sharedPrefValues = context.getSharedPreferences(
				Config.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		valuesManager = new ValuesManager(context);
		pushNumber = context.getSharedPreferences(Config.PUSH_NUMBER,
				Context.MODE_PRIVATE);
		count = pushNumber.getInt(Config.PUSH_ACCOUNT, 1);
		this.notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		keyguardManager=(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
	       
	}

	public void notify(String notificationId, String apiKey, String title,
			String message, String uri) {
		String message2 = message.substring(0, message.lastIndexOf(":"));
		String id = message.substring(message.lastIndexOf(":"),
				message.length());
		String id2 = id.substring(id.indexOf(":") + 1, id.length());
		String title1 = title.substring(0, title.indexOf("?"));
		String title2 = title.substring(title.indexOf("?") + 1, title.length());
		if (title1.contains("[")) {
			String str1 = title1.replace("[", "(");
			content = str1.replace("]", ")");
		} else {
			content = title1;
		}

		if (isNotificationEnabled()) {
			if (isNotificationToastEnabled()) {
				Toast.makeText(context, message, Toast.LENGTH_LONG).show();
			}
			Notification notification = new Notification();
			notification.icon = R.drawable.push_36x36;
			notification.number = count;
			count = count + 1;
			notification.defaults = Notification.DEFAULT_LIGHTS;
			if (valuesManager.isRing()) {
				notification.defaults |= Notification.DEFAULT_SOUND;
			}
			changeValues(Config.PUSH_ACCOUNT, count);
			if (valuesManager.isVibrator()) {
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			notification.when = System.currentTimeMillis();
			RemoteViews contentview = new RemoteViews(context.getPackageName(),
					R.layout.notification);
			
			 contentview.setImageViewResource(R.id.image,
						R.drawable.wan_push_48x48);
			contentview.setTextViewText(R.id.text, content);
			contentview.setTextViewText(R.id.tv2, message2);
			contentview.setTextViewText(R.id.tv3, title2);
			notification.contentView = contentview;
			Intent intent = new Intent(context, MatchInfoAct.class);
			intent.putExtra(IntentKey.INTENT_KEY_MATCH_ID, id2);
			intent.putExtra(IntentKey.IS_PUSH, "ispush");
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setAction(id2);
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					intent, PendingIntent.FLAG_UPDATE_CURRENT);
			notification.contentIntent = contentIntent;
			 if(keyguardManager.inKeyguardRestrictedInputMode()){
				 notificationManager.notify(1, notification);
		        }else{
		        	notificationManager.notify(random.nextInt(), notification);
		        }
			 
		} else {
		}
	}

	private boolean isNotificationEnabled() {
		return sharedPrefValues.getBoolean(
				Config.SETTINGS_NOTIFICATION_ENABLED, true);
	}

	private boolean isNotificationToastEnabled() {
		return sharedPrefValues
				.getBoolean(Config.SETTINGS_TOAST_ENABLED, false);
	}

	/**
	 * 保存条数到preferences
	 * 
	 * @param key
	 * @param values
	 */
	private void changeValues(String key, int value) {
		Editor editor = pushNumber.edit();
		editor.putInt(key, value);
		editor.commit();
	}
}
